<!--页面组件-->
<template>
  <div>
    <div class="tempxyz tableHeight systemFooter">
      <CCard class>
        <CCardHeader>
          <strong class="headerFont">风险产品归档与加载</strong>
          <div style="display: flex; float: right" @keyup.enter="getTaskList()">
            <div style="margin-right: 5px">
              <label class="labelName">任务名称&nbsp;&nbsp;</label>
              <Input
                :title="groupName"
                v-model="name"
                placeholder="请输入任务名称搜索..."
                style="width: 200px"
                maxlength="50"
              />
            </div>
            <CButton
              color="info"
              @click="createTaskBtn"
              class="system-btn-style"
            >
              <CIcon name="cil-plus" />&nbsp;任务
            </CButton>
          </div>
        </CCardHeader>
        <CCardBody class="card-body">
          <div style="height: calc(100% - 100px); margin-top: 30px">
            <Table
              border
              stripe
              max-height="680"
              ref="table"
              :columns="getColumns"
              :data="data"
            ></Table>
          </div>
          <div
            style="width: 98%;border-top: 1px solid #dcdee2;background-color: #ffffff;
              height: 70px;z-index: 1000;position: absolute;"
          >
            <Page
              :total="this.allNum"
              :current="pageNum"
              class="pageStyle"
              show-sizer
              show-elevator
              show-total
              @on-change="pageChange"
              @on-page-size-change="pageSizeChange"
            />
          </div>
        </CCardBody>
      </CCard>
    </div>

    <RiskemergencyTaskCommon
      ref="createModal"
      :modalShow="createTaskModal"
      :modalTitle="`创建`"
      @close="createTaskModal = false"
      @ok="createModalOk"
    ></RiskemergencyTaskCommon>

    <RiskemergencyTaskCommon
      ref="updateModal"
      :modalShow="updateTaskModal"
      :modalTitle="`修改`"
      @close="updateTaskModal = false"
      @ok="updateModalOk"
    ></RiskemergencyTaskCommon>

    <!-- 新建任务 -->
    <Modal v-model="addTask" title="新增任务" centered size="lg">
      <div></div>
      <template #footer class="footerBtns">
        <CButton @click="addTaskCancel" color="info" class="cancel-btn"
          >取消</CButton
        >
        <CButton @click="addTaskOk" color="info" class="system-btn-style"
          >确定</CButton
        >
      </template>
    </Modal>
    <Modal
      v-model="runShow"
      draggable
      scrollable
      title="启动任务提示"
      :styles="modalStyle"
    >
      <p slot="header" class="modalHeader">启动任务提示</p>
      <div class="modalBody">
        <p>请确认是否启动任务?</p>
      </div>
      <template #footer class="footerBtns">
        <CButton @click="runCancel" color="info" class="cancel-btn"
          >取消</CButton
        >
        <CButton @click="runOk" color="info" class="system-btn-style"
          >确定</CButton
        >
      </template>
    </Modal>
    <Modal
      v-model="banShow"
      draggable
      scrollable
      title="暂停任务提示"
      :styles="modalStyle"
    >
      <p slot="header" class="modalHeader">暂停任务提示</p>
      <div class="modalBody">
        <p>请确认是否暂停任务?</p>
      </div>
      <template #footer class="footerBtns">
        <CButton @click="banCancel" color="info" class="cancel-btn"
          >取消</CButton
        >
        <CButton @click="banOk" color="info" class="system-btn-style"
          >确定</CButton
        >
      </template>
    </Modal>
    <Modal
      v-model="delShow"
      draggable
      scrollable
      title="删除任务信息"
      :width="600"
      :styles="modalStyle"
    >
      <p slot="header" class="modalHeader">删除任务信息</p>
      <div class="modalBody">
        <p>删除节点时与任务相关的信息都会丢失</p>
        <p>请确认是否删除?</p>
      </div>
      <template #footer class="footerBtns">
        <CButton @click="delCancel" color="info" class="cancel-btn"
          >取消</CButton
        >
        <CButton @click="delOk" color="info" class="system-btn-style"
          >确定</CButton
        >
      </template>
    </Modal>
  </div>
</template>

<!--js代码-->
<script>
import RiskemergencyTaskCommon from "./common/RiskemergencyTaskCommon.vue"; 
export default {
  name: "riskProductLoading",
  data() {
    return {
      clickId: "-1",
      banShow: false,
      runShow: false,
      delShow: false,

      cronList: [],
      createTaskModal: false,
      updateTaskModal: false,

      modalStyle: { top: "340px" },

      groupName: "",
      addTask: false, //创建任务
      data: [],
      loading: true,
      currentPage: 1,
      pages: 0,
      total: 0,
      allNum: 0,
      pageNum: 1,
      pageSize: 10,
      name: "",
      getColumns: [
        //表头
        {
          type: "index",
          title: "序号",
          width: 80,
          align: "center",
        },
        {
          key: "taskName",
          title: "任务名称",
          align: "center",
          _style: "width:auto;background:rgba(241,244,249,1);",
        },
        {
          key: "sourceId",
          title: "任务类型",
          align: "center",
          _style: "width:auto;background:rgba(241,244,249,1);",
        },
        {
          key: "crontabExpressDescribe",
          title: "扫描间隔",
          align: "center",
          _style: "width:auto;background:rgba(241,244,249,1);",
        },
        {
          key: "createTime",
          title: "创建时间",
          align: "center",
          _style: "width:210px;background:rgba(241,244,249,1);",
        },
        {
          title: "操作",
          key: "show_details",
          _style: "width:220px;background:rgba(241,244,249,1);",
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small",
                  },
                  style: {
                    marginRight: "5px",
                    display: params.row.status == 1 ? "inline-block" : "none",
                  },
                  on: {
                    click: () => {
                      this.run(params.row);
                    },
                  },
                },
                "启用"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small",
                  },
                  style: {
                    marginRight: "5px",
                    display: params.row.status == 0 ? "inline-block" : "none",
                  },
                  on: {
                    click: () => {
                      this.ban(params.row);
                    },
                  },
                },
                "暂停"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small",
                  },
                  style: {
                    marginRight: "5px",
                  },
                  on: {
                    click: () => {
                      this.editBtn(params.row);
                    },
                  },
                },
                "编辑"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small",
                  },
                  on: {
                    click: () => {
                      this.del(params.row);
                    },
                  },
                },
                "删除"
              ),
            ]);
          },
        },
      ],
    };
  },
  computed: {},
  components: {
    RiskemergencyTaskCommon,
  },
  watch: {
    groupName(newVal, oldVal) {
      if (newVal.length >= 50) {
        this.$Message.warning("任务名称长度不得超过50个字符");
      }
    },
  },
  mounted() {
    this.getDataList();
    this.getCronList();
  },
  methods: {
    del(val) {
      this.delShow = true;
      this.clickId = val.id;
    },
    banCancel() {
      this.banShow = false;
    },
    delCancel() {
      this.delShow = false;
    },
    ban(val) {
      this.banShow = true;
      this.clickId = val.id;
    },
    banOk() {
      let self = this;
      // self.dataList = [];
      let path = this.$api.processingApi.updateStatus + "/" + this.clickId;
      let param = {
        status: 1, //0 启动
      };
      this.runShow = false;
      this.http.get(path, param).then((res) => {
        if (res.data.success) {
          this.banShow = false;
          this.$Message.success("暂停成功");
          this.getDataList();
        } else {
          this.$Message.error(res.data.message);
        }
      });
    },
    delOk() {
      let self = this;
      let path = this.$api.processingApi.autoTaskDelete + "/" + self.clickId;
      this.http.delete(path).then((res) => {
        if (res.data.success) {
          this.$Message.success(res.data.message);
          this.getDataList();
        } else {
          this.$Message.error(res.data.message);
        }
      });
      this.delShow = false;
    },
    run(val) {
      this.runShow = true;
      this.clickId = val.id;
    },
    runOk() {
      let self = this;
      self.dataList = [];
      let path = this.$api.processingApi.updateStatus + "/" + this.clickId;
      let param = {
        status: 0,
      };
      this.runShow = false;
      this.http.get(path, param).then((res) => {
        if (res.data.success) {
          this.getDataList();
          this.$Message.success("启动成功");
        } else {
          this.$Message.error(res.data.message);
        }
      });
    },
    runCancel() {
      this.runShow = false;
    },
    async dealWithScanInterval() {
      let _this = this;
      if (_this.cronList.length < 1) await _this.getCronList();
      for (let i = 0; i < _this.dataList.length; i++) {
        let data = _this.dataList[i];
        data.frequence = data.crontabExpress;

        let arr = _this.cronList.filter((v) => v.value === data.frequence);
        data.frequence = arr.length < 1 ? data.frequence : arr[0].label;

        let taskParameter = JSON.parse(data.taskParameter);
        data.coverageRatio = taskParameter.coverageRatio
          ? taskParameter.coverageRatio
          : 0;

        data.endTime = taskParameter.endTime;
      }

      this.$set(_this.dataList);
    },

    async getCronList() {
      let path = this.$api.processingApi.getAllChildByDictCode;
      let param = {
        dictCode: "cron",
      };

      await this.http.get(path, param).then((res) => {
        if (res.data.code == 0) {
          let dictCodeList = res.data.data.dictCodeList;
          for (let dictCode of dictCodeList) {
            let cron = {
              label: dictCode.dictName,
              value: dictCode.dictCode,
            };
            this.cronList.push(cron);
          }
        } else {
          this.$Message.error(res.data.message);
        }
      });
    },
    createTaskBtn() {
      this.createTaskModal = true;
      let data = {};
      data.cronList = this.cronList;
      this.$refs.createModal.init(data, "create");
    },
    createModalOk(data) {
      console.log(data)
      this.createTaskModal = false;
      let path = this.$api.taskManageApi.createCopy;
      this.http.post(path,data).then((res) => {
        if(res.data.success){
          this.$Message.success("创建成功");
          this.getDataList();
        } else {
          this.$Message.error("创建失败，请完整填写表单");
        }
      })
    },

    editBtn(data) {
      this.updateTaskModal = true;
      this.clickId = data.id;
      data.cronList = this.cronList;
      this.$refs.updateModal.init(data, "update");
    },

    updateModalOk(data) {
      console.log(data)
      this.updateTaskModal = false;
      console.log(this.clickId)
      let path = this.$api.taskManageApi.updateCopy + "/" + this.clickId;
      data.status = 0;
      this.http.put(path,data).then((res) => {
        if(res.data.success){
          this.$Message.success("更新成功");
          this.getDataList();
        } else {
          this.$Message.error("更新失败");
        }
      })
    },
    //表格分页
    getPageData(AllData) {
      let currentPage = this.currentPage;
      let pageSize = this.pageSize;
      this.data = [];
      let beginIndex = (currentPage - 1) * pageSize;
      let lastIndex = currentPage * pageSize - 1; //9
      for (let i = beginIndex; i < AllData.length; i++) {
        let temp = AllData[i];

        if (i > lastIndex) break;
        this.data.push(temp);
      }

      this.allNum = AllData.length;
      this.total = Math.ceil(AllData.length / this.pageSize);
      for (let i = 0; i < AllData.length; i++) {
        if (i > lastIndex) break;
        this.data[i].number = (this.currentPage - 1) * this.pageSize + i + 1;
      }
    },
    // 页码改变
    pageChange(page) {
      this.pageNum = page;
      this.currentPage = page;
      this.getPageData(this.allDataList);
    },
    //每页条数
    pageSizeChange(pageSise) {
      this.pageSize = pageSise;
      this.getPageData(this.allDataList);
    },

    // 获取表格数据
    getDataList() {
      let self = this;
      let path = this.$api.taskManageApi.getScreenDataList;
      let param = {
        name: self.name,
        // pageNum: self.pageNum,
        pageSize: self.pageSize,
      };
      this.http.get(path, param).then((res) => {
        if (res.data.code == 0) {
          self.total = Math.ceil(res.data.data.count / self.pageSize);
          let dataList = [];
          for (let i = 0; i < res.data.data.dataList.length; i++) {
            let data = res.data.data.dataList[i];
            if (data.sourceId == "12") {
              //根据sourceId来判断是否属于风险监测
              dataList.push({
                flowName: data.flowName,
                createTime: data.createTime,
                crontabExpress: data.crontabExpress,
                crontabExpressDescribe: data.crontabDescribe,
                flowId: data.flowId,
                flowParameter: data.flowParameter,
                id: data.id,
                javaBeanName: data.javaBeanName,
                lastScanTime: data.lastScanTime,
                // number: data.number,
                // runState: data.runState,
                sourceId: "应急扫描任务",
                sourceType: data.sourceType,
                status: data.status,
                taskName: data.taskName,
                taskParameter: data.taskParameter,
                userId: data.userId,
              });
            }
          }
          this.allDataList = dataList;
          this.allNum = this.allDataList.length;
          this.getPageData(this.allDataList);
        } else {
          this.$Message.error(res.data.message);
        }
      });
    },

    // 查询
    getTaskList() {
      this.currentPage = 1;
      this.getDataList();
    },

    // 创建任务
    add() {
      this.addTask = true;
    },
    addTaskOk() {},
    addTaskCancel() {},
  },
};
</script>


<!--css样式-->
<style scoped>
.myCol {
  /*margin-left: 10px;*/
  display: flex;
  align-items: center;
  padding-left: 0;
}
.search {
  display: flex;
}

.btn-sm {
  padding: 0.25rem 0.5rem;
  font-size: 0.76563rem;
  line-height: 1.5;
  border-radius: 0.2rem;
  width: 50px;
}
.table-responsive {
  height: 50% !important;
}
.labelName {
  margin-bottom: 0;
  margin-right: 5px;
  font-size: 18px;
}
.headerFont {
  font-size: 18px;
}
.groupNameWidth {
  width: 197px;
}

.ivu-radio-wrapper {
  margin-left: 0px;
}
.rowMargin {
  margin-bottom: 20px;
}
.dateMargin {
  margin-top: -5px;
}
.endMargin {
  margin-left: 19px;
}
.youxianji {
  margin-left: -5px;
}
/deep/.ivu-radio-wrapper {
  margin-bottom: 0;
}
.btn-info {
  width: 90px;
}

.clean {
  margin-left: 10px;
}
.page-link {
  cursor: pointer;
}
.pageContent {
  margin-right: 26px;
  font-size: 14px;
}
.selectStyle {
  display: inline-block;
  width: 100px;
  margin-right: 5px;
}
.footerPage {
  display: flex;
  justify-content: flex-end;
  cursor: pointer;
  color: black;
}

/* xg */
.btnStyle {
  padding: 0;
  margin: 0 3px;
}

.tablePosition {
  /* margin-top: -23px; */
  height: calc(100% - 140px);
  overflow-y: auto;
}

/deep/ .row {
  margin-right: 0;
  margin-left: 10px;
}

/deep/ .footerPage {
  padding-right: 0;
}

/deep/.ivu-table td.centerText {
  text-align: center;
  text-align: -moz-center;
  text-align: -webkit-center;
}

.card-body {
  overflow-y: auto;
  height: calc(100% - 52px) !important;
}
</style>
<style scoped>
.tempxyz .card-body {
  position: static;
  padding: 0 20px;
}

/deep/.form-control {
  padding: 0;
}
.pageStyle {
  position: absolute;
  right: 25px;
  margin-top: 15px;
}
</style>
